package com.hefei.customer.login.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JsonNode;

import com.hefei.common.http.response.BaseResponse;
import com.hefei.common.returncode.ReturnCode;
import com.hefei.common.util.JacksonUtil;
import com.hefei.customer.login.po.User;
import com.hefei.customer.login.service.ILoginService;
import com.hefei.service.framework.base.request.RequestThreadLocal;
import com.hefei.service.framework.exception.ServiceException;

@Controller
@RequestMapping(value="/login", produces = "text/plain;charset=UTF-8")
public class LoginController {
	
	private static final Logger logger = Logger.getLogger(LoginController.class);
	
	@Autowired
	private ILoginService loginService;
	
	@RequestMapping(value="/Vlogin",produces="text/plain;charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody
	public String getUserByname(){
		
		BaseResponse result = new BaseResponse();
		result.setReturnCode(ReturnCode.RETURN_CODE_ERROR);
		String plain = RequestThreadLocal.getPlain();
		
		try {
			//参数判断非空
			if(StringUtils.isBlank(plain)) {
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
				logger.info(RequestThreadLocal.getLoggerStr() + " Plain is null ");
				return JacksonUtil.beanToJson(result);
			}
			String username = null;
			JsonNode jsonNode = JacksonUtil.getJsonNode(plain);
			if(jsonNode.has("username")){
				username = jsonNode.get("username").asText();
			}
			
			if(StringUtils.isNotBlank(username) ){
				User user = loginService.getUserByname(username);
				result.setReturnCode(ReturnCode.RETURN_CODE_SUCCESS);
				result.setReturnObject(user);
			}else{
				result.setReturnCode(ReturnCode.RETURN_CODE_ERROR_PARAM_NULL);
			}
			
		} catch (ServiceException e) {
			result.setReturnCode(e.getErrorCode());
		}
		
		logger.info(RequestThreadLocal.getLoggerStr()+" result "+result);
		return JacksonUtil.beanToJson(result);
		
	}
	
	

}
